package cn.moremind.leetcode;

/**
 * @author <a href="mailto:hefe@bsoft.com.cn">hefengen</a>
 * @(#) Created on
 * 版权：版权所有 bsoft 保留所有权力。
 * @description
 */

import java.io.*;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class FileDivisionTest {

    /**
     * 分割文件
     * @param name
     * @param startIndex
     * @param temp
     * @throws IOException
     */
    public void divisionFile(String name, Long startIndex, File temp) throws IOException {
        Long startMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println(Thread.currentThread().getName() + "开始写：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        File file = new File(name);
        if (file.exists()) {
            file.delete();
        }
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();

        FileOutputStream writer = new FileOutputStream(file);
        RandomAccessFile accessFile = new RandomAccessFile(temp, "r");
        byte[] chars = new byte[1024 * 1024];
        long readStart = startIndex * DemoApplicationTests.defaultReadSize;
        long readEnd = (startIndex+1) * DemoApplicationTests.defaultReadSize;
        for (int i = 0; i < DemoApplicationTests.defaultFileSize; i++) {
            if (readStart >= readEnd) {
                break;
            }
            accessFile.seek(readStart);
            int read = accessFile.read(chars);
            if (read > 0) {
                writer.write(chars, 0, read);
                readStart = readStart + read;
            } else {
                break;
            }
        }
        writer.flush();
        writer.close();
        Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println(Thread.currentThread().getName() + "结束写：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "   " + (endMilliSecond - startMilliSecond) / 1000);
    }

    /**
     * 还原文件
     * @param name
     * @param startIndex
     * @param temp
     * @throws IOException
     */
    public void heFile(String name, Long startIndex, File temp) throws IOException {
        Long startMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println(Thread.currentThread().getName() + "开始读：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        File file = new File(name);
        if (!file.exists()) {
            return;
        }

        FileInputStream inputStream = new FileInputStream(file);
        RandomAccessFile accessFile = new RandomAccessFile(temp, "rw");
        byte[] chars = new byte[1024 * 1024];
        long readStart = startIndex * DemoApplicationTests.defaultReadSize;
        long readEnd = (startIndex+1) * DemoApplicationTests.defaultReadSize;
        for (int i = 0; i < DemoApplicationTests.defaultFileSize; i++) {
            if (readStart >= readEnd) {
                break;
            }
            accessFile.seek(readStart);
            int read = inputStream.read(chars);
            if (read > 0) {
                accessFile.write(chars, 0, read);
                readStart = readStart + read;
            } else {
                break;
            }
        }
        accessFile.close();
        Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println(Thread.currentThread().getName() + "开始读：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "   " + (endMilliSecond - startMilliSecond) / 1000);
    }
}


